package com.cecw.Utils;

import com.cecw.pojo.Project;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;

import java.net.HttpURLConnection;
import java.util.Date;

/**
 * @author linlingxin
 * @date 2024/5/24 15:41
 */

public class GetInfoUtil1 {

    private GetInfoUtil1() {
    }

    public static Project getInfo(Project project, String url, String bearerToken){

        try {

            CloseableHttpClient httpClient = HttpClients.createDefault();

            // 然后把编码结果加到请求头里面去
            HttpGet request = new HttpGet(url);
            request.setHeader("Authorization", "Bearer " + bearerToken);

            HttpResponse response = httpClient.execute(request);

            // 检查响应状态码
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpURLConnection.HTTP_OK) {
                String responseBody = EntityUtils.toString(response.getEntity());
                // 使用 Jackson 解析 JSON
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode rootNode = objectMapper.readTree(responseBody);
                // 获取指定键的值
                JsonNode valueNode = rootNode.path("rows");
                if(valueNode != null){
                    // 如果有多项目
                    if(valueNode.size() > 1){
                        project.setIsMany(1);
                    }
                    for (JsonNode jsonNode : valueNode) {
                        String status = jsonNode.path("alertStatus").asText();
                        String productCode = jsonNode.path("productCode").asText();
                        if(status.equals("CLOSED") || status.equals("PENDING")){
                            project.setProjectCode(productCode);
                            continue;
                        }
                        String serviceName = jsonNode.path("serviceName").asText();
                        String projectKey = jsonNode.path("projectKey").asText();
                        String[] split = projectKey.split(":");
                        projectKey = split[0];
                        project.setProjectCode(productCode);
                        project.setProjectKey(projectKey);
                        project.setServiceName(serviceName);
                        project.setStatus(status);
                        // 如果有多个就加上原来的
                        project.setnCloc(project.getnCloc()+jsonNode.path("line").asInt());
                    }
                    project.setCreateTime(new Date());
                    return project;
                }
            } else {
                System.out.println("HTTP 请求失败：" + statusCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
